const db=require('./db.js');
const crypto=require('crypto');
let md5=crypto.createHash('md5');

const account={};

account.passwordCheck=(req,callback)=>{
    let account=req.body.account;
    let sql_password="select password from accounts where account=?";
    db.query(sql_password,account,(err,result)=>{
        if(err){
            callback({err:{message:'账户不存在'},});
        }else{
            //数据库返回的结果数组中第一个为包含密码键值对的对象
            let password =result[0].password;
            let z_pas=md5.update(req.body.password).digest('base64');
            if(password==z_pas){
                req.session.account=account;
                req.session.login=true;
                callback({success:true});
            }else{
                callback({err:{message:'密码错误',u:z_pas,t:password}});
            }
        }
    })
}

account.loginCheck=(req,callback)=>{
    if(req.login){
        callback({message:true});
    }else{
        callback({message:false});
    }
}

module.exports=account;